﻿namespace Bitcoin
{
    public class ValidationState
    {
        public enum ModeState
        {
            Valid,
            Invalid,
            Error
        }

        public const byte REJECT_CHECKPOINT = 0x43;

        public const byte REJECT_DUPLICATE = 0x12;
        public const byte REJECT_DUST = 0x41;
        public const byte REJECT_INSUFFICIENTFEE = 0x42;
        public const byte REJECT_INVALID = 0x10;
        public const byte REJECT_MALFORMED = 0x01;
        public const byte REJECT_NONSTANDARD = 0x40;
        public const byte REJECT_OBSOLETE = 0x11;

        public ValidationState()
        {
        }

        public bool CorruptionPossible { get; private set; }
        public int DoS { get; private set; }
        public ModeState Mode { get; private set; }
        public byte RejectCode { get; private set; }
        public string RejectReason { get; private set; }

        public void AddDoS(int level, byte rejectCode, string rejectReason, bool corruptionPossible)
        {
            RejectCode = rejectCode;
            RejectReason = rejectReason;
            CorruptionPossible = corruptionPossible;
            if (Mode == ModeState.Error) return;

            DoS += level;
            Mode = ModeState.Invalid;
        }

        public void AddError(string rejectReason)
        {
            if (Mode == ModeState.Valid)
            {
                RejectReason = rejectReason;
            }
            Mode = ModeState.Error;
        }

        public void AddInvalid(byte rejectCode, string rejectReason)
        {
            AddDoS(0, rejectCode, rejectReason, false);
        }
    }
}